/**
 * 用户首页配置
 * <p>Title: ServiceZM91.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 高强
 * @version 1.0
 * <pre>Histroy:
 *          2012-12-12 下午05:23:37 高强  Create
 *</pre>
 */
package com.baosight.sczx.zm.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.CmesConstants;
import com.baosight.sczx.zm.domain.ZM91;

/**
 * <p>Title: ServiceZM91.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 高强
 * @version 1.0
 * <pre>Histroy:
 *          2012-12-12 下午05:23:37 高强  Create
 *</pre>
 */
public class ServiceZM91 extends ServiceEPBase{
	
	/**
	 * 初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *     2012-11-12 上午10:00:31  高强  
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public EiInfo initLoad(EiInfo info)
	{		
		EiInfo outInfo = new EiInfo();
		EiBlock initBlock = new EiBlock("result");
		initBlock.addBlockMeta(new ZM91().eiMetadata);
		Map<String,String> map = new HashMap<String,String>();
		map.clear();
		List initList = dao.query("ZM91.initQuery",map);
		initBlock.addRows(initList);
		outInfo.addBlock(initBlock);
		
		EiBlock userBlock = new EiBlock("userInfo");
		userBlock.addBlockMeta(new ZM91().eiMetadata);
		map.put("fdUserId", UserSession.getLoginName());
		map.put("fdOrgCode", UserSession.getInSessionProperty("orgCode") ==null? "":UserSession.getInSessionProperty("orgCode").toString());
		List resultList = dao.query("ZM91.userQuery",map);
		userBlock.addRows(resultList);
		outInfo.addBlock(userBlock);
		
		EiBlock resgGridBlock = new EiBlock("resgGrid");
		resgGridBlock.addBlockMeta(new ZM91().eiMetadata);
		List resgGridList = dao.query("ZM91.gridQuery",map);
		resgGridBlock.addRows(resgGridList);
		outInfo.addBlock(resgGridBlock);
		
		EiBlock resgGridAddBlock = new EiBlock("resgGridAdd");
		resgGridAddBlock.addBlockMeta(new ZM91().eiMetadata);
		List resgGridAddList = dao.query("ZM91.gridUserQuery",map);
		resgGridAddBlock.addRows(resgGridAddList);
		outInfo.addBlock(resgGridAddBlock); 
		
		EiBlock isLeaderBlock = new EiBlock("isLeader");
		isLeaderBlock.addBlockMeta(new ZM91().eiMetadata);
		List isLeaderList = dao.query("ZM91.query",map);
		isLeaderBlock.addRows(isLeaderList);
		outInfo.addBlock(isLeaderBlock); 
		return outInfo ;
	}
	
	 /**
	 * 保存
	 * <p>Function: save</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *     2012-11-12 上午10:00:31  高强  
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public EiInfo save(EiInfo info)
	{//先行删除，再行添加
		EiBlock blockForDelete = new EiBlock("blockForDelete");
		EiColumn column = new EiColumn("fdUserId");
		blockForDelete.addMeta(column);
		info.addBlock(blockForDelete);
		Map map = new HashMap();
		map.put("fdUserId", UserSession.getLoginName());
		blockForDelete.addRow(map);
		super.update(info,"ZM91.deleteByUserId","blockForDelete");
			
		if(info.getBlock("checkBlockSave")!= null)
		{
			EiBlock block = info.getBlock("checkBlockSave");
			
			for(int i = 0; i < block.getRowCount();i++)
			{
				String primKey = getPrimKey16(); 
				Map row = block.getRow(i);
				row.put("fdInitialdatadetailId", row.get("fdKeyId").toString());
				row.put("fdKeyId", primKey);
				row.put("fdUserId", UserSession.getLoginName().toString());		
				row.put("fdOrgCode", UserSession.getInSessionProperty("orgCode") ==null? "":UserSession.getInSessionProperty("orgCode").toString());		
				row.put("fdValue", "Y");
			}
			if(block.getRowCount() > 0)
			{
				super.update(info,"ZM91.insert","checkBlockSave");	
			}		
		}
		if(info.getBlock("resgGridAdd")!= null)
		{
			EiBlock block = info.getBlock("resgGridAdd");
			
			for(int i = 0;i < block.getRowCount(); i++)
			{
				String primKey = getPrimKey16(); 
				Map row = block.getRow(i);
				row.put("fdInitialdatadetailId", row.get("fdKeyId").toString());
				row.put("fdKeyId", primKey);
				row.put("fdUserId", UserSession.getLoginName().toString());
				row.put("fdOrgCode", UserSession.getInSessionProperty("orgCode") ==null? "":UserSession.getInSessionProperty("orgCode").toString());	
				row.put("fdValue", Integer.toString(i));
				row.put("fdIshyperlink", row.get("fdIshyperlink").toString());
				row.put("fdHyperlinkName", getHypterLinkName(info.getBlock("isLeader"),row.get("fdIshyperlink").toString(),row.get("fdModuleName").toString()));
				row.put("fdHyperlinkViewName", getHypterLinkViewName(info.getBlock("isLeader"),row.get("fdIshyperlink").toString(),row.get("fdModuleName").toString()));
			}
			if(block.getRowCount() > 0)
			{
				super.update(info,"ZM91.insert","resgGridAdd");
			}
		}
		
		if(info.getBlock("isLeader")!= null)
		{
			EiBlock block = info.getBlock("isLeader");
			String primKey = getPrimKey16(); 
			Map<String, String> _argMap = new HashMap<String, String>();
			_argMap.put("fdKeyId", primKey);
			_argMap.put("fdInitialdatadetailId", primKey);
			_argMap.put("fdUserId", UserSession.getLoginName().toString());
			_argMap.put("fdOrgCode", UserSession.getInSessionProperty("orgCode") ==null? "":UserSession.getInSessionProperty("orgCode").toString());	
			_argMap.put("fdValue",block.getRow(0).get("fdValue").toString() );
			dao.insert("ZM91.insert", _argMap);
		}
		changeInitialData(info);
		return info ;
	}
	
	 /**
	 * 获得主键
	 * <p>Function: getPrimKey16</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *     2012-11-12 上午10:00:31  高强  
	 * </p>
	 *
	 */
	 private String getPrimKey16()
	 {
		 return TablePk.getInstance().getNextValue16();
	 }
	 
	 /**
	 * 如果修改成功则刷新数据
	 * <p>Function: changeInitialData</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-11-12 上午10:00:31  高强  
	 * </p>
	 *
	 */
	private void changeInitialData(EiInfo outInfo){
		if(outInfo != null && outInfo.getStatus() != -1) {
			CmesConstants.INDEXCONFIGER = 1;
		}
	}
		
	/**
	 * 获得超链接名称
	 * <p>Function: getHypterLinkName</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-12-12 上午10:00:31  高强  
	 * </p>
	 *
	 */
	private String getHypterLinkName(EiBlock leaderBlock,String isHypterLink,String moduleName)
	{
		String hypterLinkName ="";
		if(isHypterLink.equals("有"))
		{
			if(moduleName.equals("材料消耗统计")||moduleName.equals("当月出入库情况")||moduleName.equals("安全库存")||moduleName.equals("设备提醒"))
			{
				hypterLinkName = "更多";
			}else
			{
				if(leaderBlock.getRow(0).get("fdValue").toString().equals("是"))
				{
					hypterLinkName = "更多";
				}else
				{
					hypterLinkName = "处理";
				}
			}
		}
		
		return hypterLinkName;
	}
		
	/**
	 * 获得超链接要打开画面名称
	 * <p>Function: getHypterLinkViewName</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-12-12 上午10:00:31  高强  
	 * </p>
	 *
	 */
	private String getHypterLinkViewName(EiBlock leaderBlock,String isHypterLink,String moduleName)
	{
		String viewName ="";
		if(isHypterLink.equals("有"))
		{
			if(moduleName.equals("材料消耗统计"))
			{
				viewName ="ZM9W";
			}
			else if(moduleName.equals("安全库存"))
			{
				viewName ="ZMA9";
			}
			else if(moduleName.equals("当月出入库情况"))
			{
				viewName ="ZM9R";
			}
			else if(moduleName.equals("设备提醒"))
			{
				viewName ="ZM9B";
			}
			
		}
		return viewName;
	}
}
